package com.myproject.thread.volatiles;

import com.myproject.entity.Student;

import java.util.HashMap;
import java.util.Map;

//引用数据类型volatile测试
public class Volatile {

    //private static volatile Student stu = new Student();
    private static volatile Map map = new HashMap<String,String>();
    private static Student stu = new Student();


    public static void main(String[] args) throws InterruptedException {

        map.put("flag","true");

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("t1 start");
                while ("true".equals(map.get("flag"))){}
                System.out.println("t1 end");
            }
        });
        t1.start();
        Thread.sleep(3000);
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("t2 start");
                map.put("flag","false");
                System.out.println("t2 end");
            }
        });
        t2.start();
    }
}
